package com.world.blog.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.world.blog.factory.BlogAsyncManager;
import com.world.blog.util.VideoUtil;
import com.world.common.constant.CacheConstants;
import com.world.common.core.domain.entity.SysUser;
import com.world.common.core.redis.RedisCache;
import com.world.common.core.service.BaseService;
import com.world.common.utils.DateUtils;
import com.world.common.utils.SecurityUtils;
import com.world.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.world.blog.mapper.ProjectMapper;
import com.world.blog.domain.BlogProject;
import com.world.blog.service.IBlogProjectService;

/**
 * 项目Service业务层处理
 * 
 * @author LiJunXi
 * @date 2025-05-25
 */
@Service
public class BlogProjectServiceImpl extends BaseService<BlogProject> implements IBlogProjectService
{
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private BlogAsyncManager blogAsyncManager;

    /**
     * 查询项目
     * 
     * @param projectId 项目主键
     * @return 项目
     */
    @Override
    public BlogProject selectProjectByProjectId(Long projectId)
    {
        BlogProject blogProject = projectMapper.selectProjectByProjectId(projectId);
        SysUser user = userService.selectUserById(blogProject.getUserId());
        fillProjectUiParams(blogProject, user);
        blogAsyncManager.addProjectViewsCount(projectId);
        return blogProject;
    }

    @Override
    public Integer getProjectViewsCount(Long projectId) {
        Integer viewsCount = redisCache.getCacheObject(CacheConstants.BLOG_PROJECT_VIEWS_COUNT + projectId);
        return Objects.isNull(viewsCount) ? 0 : viewsCount;
    }

    private void fillProjectUiParams(BlogProject blogProject, SysUser user) {
        blogProject.setNickName(user.getNickName());
        blogProject.setQq(user.getQq());
        blogProject.setWechat(user.getWechat());
        blogProject.setViewsCount(getProjectViewsCount(blogProject.getProjectId()));
    }

    /**
     * 查询项目列表
     * 
     * @param blogProject 项目
     * @return 项目
     */
    @Override
    public List<BlogProject> selectProjectList(BlogProject blogProject)
    {
        List<BlogProject> blogProjects = projectMapper.selectProjectList(blogProject);
        List<Long> userIds = blogProjects.stream().map(BlogProject::getUserId).collect(Collectors.toList());
        Map<Long, SysUser> userMap = userService.selectUserList(userIds)
                .stream()
                .collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
        return pageList2CustomList(blogProjects, (List<BlogProject> list) -> {
            return list.stream().map(item -> {
                SysUser user = userMap.get(item.getUserId());
                fillProjectUiParams(item, user);
                return item;
            }).collect(Collectors.toList());
        });
    }

    /**
     * 新增项目
     * 
     * @param blogProject 项目
     * @return 结果
     */
    @Override
    public int insertProject(BlogProject blogProject)
    {
        blogProject.checkSaveParams();
        fillProjectSaveParams(blogProject);
        return projectMapper.insertProject(blogProject);
    }

    private void fillProjectSaveParams(BlogProject blogProject) {
        blogProject.setUserId(SecurityUtils.getUserId());
        blogProject.setViewsCount(0);
        blogProject.setCreateTime(DateUtils.getNowDate());
        if (StrUtil.isNotEmpty(blogProject.getVideoUrl())) {
            blogProject.setVideoUrl(VideoUtil.getStopAutoplayVideoUrl(blogProject.getProjectUrl()));
        }
    }

    /**
     * 修改项目
     * 
     * @param blogProject 项目
     * @return 结果
     */
    @Override
    public int updateProject(BlogProject blogProject)
    {
        blogProject.setUpdateTime(DateUtils.getNowDate());
        return projectMapper.updateProject(blogProject);
    }

    /**
     * 批量删除项目
     * 
     * @param projectIds 需要删除的项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectIds(Long[] projectIds)
    {
        return projectMapper.deleteProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目信息
     * 
     * @param projectId 项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectId(Long projectId)
    {
        return projectMapper.deleteProjectByProjectId(projectId);
    }
}
